package com.itheima.service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.JobOption;
import com.itheima.pojo.StuClazzCount;
import com.itheima.pojo.StudentCountData;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.service.impl
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 员工职位统计
     *
     * @return
     */
    /*@Override
    public JobOption getEmpJobData() {
        // 统计查询
        List<JobData> jobDataList = empMapper.empJobData();
        JobOption jobOption = new JobOption();
        //"jobList": ["教研主管","学工主管","其他","班主任","咨询师","讲师"],
        // 遍历jobDataList,取出每个数据的pos，就代表职位
        *//*List<String> jobList = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();
        for (JobData jobData : jobDataList) {
            String pos = jobData.getPos();
            jobList.add(pos);
            dataList.add(jobData.getTotal());
        }*//*

        // stream中的map, 映射，把流中元素转换成另一种类型
        List<String> jobList = jobDataList.stream()
            .map(JobData::getPos) // getPos的返回值是String, 流中元素就被转成了String
            // 收集流中的每个元素，装起来，用什么来装，用List集合, Collectors: 收集器, toList: 收集器中的List容器
            .collect(Collectors.toList());
        List<Integer> dataList = jobDataList.stream()
            .map(JobData::getTotal) // getTotal的返回值是Integer, 流中元素就被转成了Integer
            .collect(Collectors.toList());

        jobOption.setJobList(jobList);// 字符串集合
        //    "dataList": [1,1,2,6,8,13]
        jobOption.setDataList(dataList);

        return jobOption;
    }*/
    @Override
    public JobOption getEmpJobData() {
        // 统计查询
        List<Map<String,Object>> jobDataList = empMapper.empJobDataMap();
        List<String> jobList = jobDataList.stream().// 流中元素是map, 取职位的值
            map(map -> map.get("pos").toString())// 职位的字符串
            .collect(Collectors.toList());
        List<Integer> dataList = jobDataList.stream().// 流中元素是map, 取职位的值
            map(map -> Integer.parseInt(map.get("total").toString()))// 职位的字符串
            .collect(Collectors.toList());

        JobOption jobOption = new JobOption();
        jobOption.setJobList(jobList);
        jobOption.setDataList(dataList);
        return jobOption;
    }

    /**
     * 按员工性别统计数量
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getEmpGenderData() {
        return empMapper.getEmpGenderData();
    }

    @Override
    public StudentCountData studentCountData() {
        StudentCountData studentCountData = new StudentCountData();
        List<StuClazzCount> clazzList = studentMapper.countByClazz();
        if (!CollectionUtils.isEmpty(clazzList)) {
            List<String> clazzNameList = clazzList.stream().map(StuClazzCount::getClazzName).collect(Collectors.toList());
            List<Integer> dataList = clazzList.stream().map(StuClazzCount::getCount).collect(Collectors.toList());
            studentCountData.setClazzList(clazzNameList);
            studentCountData.setDataList(dataList);
        }
        return studentCountData;
    }

    @Override
    public List<Map<String, Object>> studentDegreeData() {
        return studentMapper.studentDegreeData();
    }
}
